package de.sscoolapps.vokabeltrainer;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

public class DBHelper {
	private static final String DATABASE_NAME = "muster1.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "tab1"; 
	private Context context;
	private SQLiteDatabase db;
	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)";
	
	public DBHelper(Context context) { 
		this.context = context;
		OeffnenHelfer oeffnenHelfer = new OeffnenHelfer(this.context);
		this.db = oeffnenHelfer.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
		}
	
	public long insert(String name) {
		this.insertStmt.bindString(1, name);
		return this.insertStmt.executeInsert();
		}
	
	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
		}
	
	public List <String> selectAll() {
		List<String> list = new ArrayList<String>();
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, null, null, null, null, "name desc");
		if (cursor.moveToFirst()) {
			do { list.add(cursor.getString(0));
			} 
			while (cursor.moveToNext());
			} 
		if (cursor != null && !cursor.isClosed()) {
			cursor.close(); }
		return list;
		}
	
	private static class OeffnenHelfer extends SQLiteOpenHelper {
		
		OeffnenHelfer(Context context) { 
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
			}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);
			}
		}
}



